//
//  MYGCasefileSearchViewController.swift
//  MYGrow
//
//  Created by 王振明 on 2024/4/21.
//

import UIKit

class MYGCasefileSearchViewController: MYGBaseController {
    var historyArr:Array<String>!

    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    @objc func clickBackBtn(){
        navigationController?.popViewController(animated: true)
    }
    @objc func labTapAction(tap:UITapGestureRecognizer){
        let lab:UILabel = tap.view as! UILabel
        headSearchBar.text = lab.text
        if historyArr.contains(lab.text ?? ""){
            historyArr.removeAll(lab.text ?? "")
        }
        historyArr.insert(lab.text ?? "", at: 0)
        UserDefaults.standard.setValue(historyArr, forKey: "caseFileSearchHistory")
        let vc = MYGCaseSearchResultViewController()
        vc.searchContent = lab.text ?? ""
        self.navigationController?.pushViewController(vc)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    func configUI(){
        view.backgroundColor = .white
        view.addSubview(backBtn)
        backBtn.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(statusBarHeight)
            make.size.equalTo(CGSize(width: 44, height: 44))
        }
        view.addSubview(headSearchBar)
        headSearchBar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(44)
            make.centerY.equalTo(backBtn)
            make.height.equalTo(SCALEWIDTH(30))
            make.right.equalToSuperview().offset(-SCALEWIDTH(20))
        }
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(20))
            make.top.equalTo(backBtn.snp.bottom).offset(SCALEWIDTH(30))
            make.height.equalTo(SCALEWIDTH(13))
        }
        view.addSubview(scView)
        scView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(SCALEWIDTH(20))
        }
        scView.addSubview(containerView)
        containerView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(3000)
            make.width.equalTo(screenWidth)
            make.bottom.equalToSuperview()
        }
        createHistoryView()
    }
    private func createHistoryView(){
        historyArr = UserDefaults.standard.object(forKey: "caseFileSearchHistory") as? Array ?? []
        var maxHeight = SCALEWIDTH(40)
        var nowXPoint = SCALEWIDTH(20)
        for ind in 0..<historyArr.count {
            let lab = UILabel()
            lab.text = historyArr[ind]
            lab.font = .regular(13)
            lab.textColor = .rgba(36, 36, 36)
            lab.textAlignment = .center
            lab.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(labTapAction(tap:)))
            lab.addGestureRecognizer(tap)
            lab.backgroundColor = .rgba(247, 247, 247)
            lab.layerCornerRadius = SCALEWIDTH(13.5)
            let size = getStringSize(font: .regular(13), viewSize: CGSize(width: CGFloat(MAXFLOAT), height: SCALEWIDTH(27)), str: historyArr[ind])
            if nowXPoint + size.width + SCALEWIDTH(32) > screenWidth - SCALEWIDTH(20) {
                nowXPoint = SCALEWIDTH(20)
                maxHeight = maxHeight + SCALEWIDTH(40)
            }
            lab.frame = CGRect(x: nowXPoint, y: maxHeight - SCALEWIDTH(27), width: size.width + SCALEWIDTH(32), height: SCALEWIDTH(27))
            containerView.addSubview(lab)
            nowXPoint = nowXPoint + SCALEWIDTH(32) + SCALEWIDTH(12) + size.width
        }
        containerView.snp.updateConstraints { make in
            make.height.equalTo(maxHeight)
        }
    }
    
    private lazy var backBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Common/back"), for: .normal)
        btn.imageView?.tintColor = .rgba(51, 51, 51, 1)
        btn.addTarget(self, action: #selector(clickBackBtn), for: .touchUpInside)
        return btn
    }()
    private lazy var headSearchBar:UITextField = {
       let cont = UITextField()
        cont.placeholder = "🔍搜索"
        cont.backgroundColor = .rgba(243, 243, 243)
        cont.textAlignment = .center
        cont.font = .regular(13)
        cont.returnKeyType = .search
        cont.delegate = self
        cont.layerCornerRadius = SCALEWIDTH(15)
        cont.layerShadowColor = .rgba(0, 0, 0, 1)
        cont.layer.shadowOffset = CGSize(width: 1, height: 1)
        cont.layer.shadowRadius = 7
        cont.layer.shadowOpacity = 1
        return cont
    }()
    lazy var titleLabel:UILabel = {
        let cont = UILabel()
        cont.font = .medium(15)
        cont.textColor = .rgba(51, 51, 51,1)
        cont.text = "搜索历史"
        return cont
    }()
    lazy var scView:UIScrollView = {
       let cont = UIScrollView()
        return cont
    }()
    lazy var containerView:UIView = {
       let cont = UIView()
        return cont
    }()
}
extension MYGCasefileSearchViewController:UITextFieldDelegate{
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField.text?.count ?? 0 > 0{
            if historyArr.contains(textField.text ?? ""){
                historyArr.removeAll(textField.text ?? "")
            }
            historyArr.insert(textField.text ?? "", at: 0)
            UserDefaults.standard.setValue(historyArr, forKey: "caseFileSearchHistory")
            let vc = MYGCaseSearchResultViewController()
            vc.searchContent = textField.text ?? ""
            self.navigationController?.pushViewController(vc)
        }
        return true
    }
}
